#include <stdio.h>
#include <stdlib.h>

typedef struct Node
{
    int data;
    struct Node *next;
} Node_t, *Node_p;

Node_p Node_Init()
{
    Node_p head = (Node_p)malloc(sizeof(Node_t));
    head->next = NULL;
    return head;
}

// 头插法
void Node_Insert(Node_p head, int data)
{
    Node_p p = (Node_p)malloc(sizeof(Node_t));
    p->data = data;
    p->next = head->next;
    head->next = p;
}

// 尾插法
void Node_Insert_Tail(Node_p head, int data)
{
    Node_p p = head;
    while (p->next != NULL)
    {
        p = p->next;
    }
    Node_p q = (Node_p)malloc(sizeof(Node_t));
    q->data = data;
    q->next = NULL;
    p->next = q;
}

void Node_Print(Node_p head)
{
    Node_p p = head->next;
    while (p != NULL)
    {
        printf("%d ", p->data);
        p = p->next;
    }
    printf("\n");
}

void delete_node(Node_p head, int data)
{
    Node_p p = head;
    while (p->next != NULL)
    {
        if (p->next->data == data)
        {
            p->next = p->next->next;
            free(p->next);
        }
        p = p->next;
    }
}

int find_node(Node_p head, int data)
{
    int index = 0;
    Node_p p = head->next;
    while (p != NULL)
    {
        if (p->data == data)
        {
            return index;
        }
        index++;
        p = p->next;
    }
    return -1;
}
int main()
{
    Node_p head = Node_Init();
    // Node_Insert(head, 1);
    // Node_Insert(head, 2);
    // Node_Insert(head, 3);
    // Node_Insert(head, 4);

    Node_Insert_Tail(head, 1);
    Node_Insert_Tail(head, 2);
    Node_Insert_Tail(head, 3);
    Node_Insert_Tail(head, 4);
    // printf("find %d\n", find_node(head, 2));

    delete_node(head, 1);
    // printf("find %d\n", find_node(head, 2));
    delete_node(head, 2);
    // printf("find %d\n", find_node(head, 2));
    Node_Print(head);
    return 0;
}